package org.example.common;

import lombok.Data;

@Data
public class Response<T> {

    // 状态码
    private int code;

    // 返回信息
    private String message;

    // 数据部分
    private T data;

    // 默认构造函数
    public Response() {
    }

    // 构造函数
    public Response(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 构造成功的响应
    public static <T> Response<T> success(T data) {
        return new Response<>(200, "操作成功", data);
    }

    // 构造失败的响应
    public static <T> Response<T> error(int code, String message) {
        return new Response<>(code, message, null);
    }

    // 构造失败的响应，默认错误码为 500
    public static <T> Response<T> error(String message) {
        return new Response<>(500, message, null);
    }


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
